<template>
  <div id="pro_recommend">
    <div id="system_role">
      <!--顶部搜索框内容开始-->
      <div class="role_seek">
        <el-select v-model="forms.recom" placeholder="请选择推荐类型">
          <el-option
            v-for="item in recommendType"
            :key="item.recomData"
            :label="item.recomName"
            :value="item.recomData"
          >
          </el-option>
        </el-select>

        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="seek"
          v-power="'promote:recommend:query'"
          >查找</el-button
        >
        <el-button size="mini" type="primary" icon="el-icon-edit" @click="add"
        v-power="'promote:recommend:create'"
          >添加</el-button
        >
        <!--新增添加弹出框开始-->
        <el-dialog title="创建" :visible.sync="dialogFormVisible">
          <el-form
            :model="form"
            :rules="rules"
            ref="form"
            class="pop-up"
            status-icon
          >
            <el-form-item label="推荐类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择">
                <el-option
                  v-for="item in recommendType"
                  :key="item.recomData"
                  :value="item.recomData"
                  :label="item.recomName"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="推荐商品" prop="goods">
              <el-cascader
                v-model="form.goods"
                :options="spulist"
                ref="dom"
              ></el-cascader>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancelbutton('form')">取 消</el-button>
            <el-button type="primary" @click="adduser('form')">确 定</el-button>
          </div>
        </el-dialog>
        <!--新增添加弹出框结束-->
      </div>
      <!--顶部搜索框内容结束-->

      <!--底部列表内容部分-->
      <template>
        <el-table
          border
          :data="tableData"
          style="width: 100%"
          :default-sort="{ prop: 'date', order: 'descending' }"
        >
          <el-table-column
            prop="id"
            label="推荐ID"
            sortable
            align="center"
          ></el-table-column>
          <el-table-column
            prop="spuTitle"
            label="推荐商品"
            align="center"
          ></el-table-column>
          <el-table-column prop="recommendType" label="推荐类型" align="center">
            <template slot-scope="scope">
              <el-tag>{{ scope.row.recommendType | recommendTypeMe }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                class="button_remove"
                size="mini"
                type="danger"
                 v-power="'promote:recommend:delete'"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!--底部列表内容部分结束-->

      <!--底部分页开始-->
      <div class="block">
        <Paging :forms="forms" :gain="gain"></Paging>
      </div>
      <!--底部分页结束-->
    </div>
  </div>
</template>

<script>
import Paging from "../../components/Paging.vue"; //自定义组件
import Recommend from "@/api/promotion/Recommend";
export default {
  data() {
    //验证推荐类型是否为空
    var type = (rule, value, callback) => {
      if (value === "" || value === null) {
        //非空验证
        callback(new Error("推荐类型"));
      } else {
        callback();
      }
    };
    //推荐商品是否为空
    var goods = (rule, value, callback) => {
      if (value === "") {
        //非空验证
        callback(new Error("推荐商品"));
      } else {
        callback();
      }
    };
    return {
      spulist: [],

      //弹出框
      tableData: [{}], //table里面的数据
      dialogFormVisible: false, //控制弹出框是否显示
      AuthorizaPopup: false,
      formLabelWidth: "120px", //弹出框宽度
      bool: true, //布尔值 判断编辑还是修改 为true是创建 否则就是修改

      //分页
      forms: {
        pageSize: 10, //limit
        total: 0, //分页——-共几条
        pageNo: 1, //页码
        recom: ""
      },
      recommendType: [
        {
          recomData: 1,
          recomName: "橱窗推荐"
        },
        { recomData: null, recomName: "全部" }
      ],
      form: {
        type: "", //推荐类型
        goods: "" //推荐商品
      },

      //弹出框验证
      rules: {
        type: [{ required: true, validator: type, trigger: "change" }], //如果角色名称验证是正确的就给蓝色
        goods: [{ required: true, validator: goods, trigger: "change" }] //如果说明验证是正确的就给蓝色
      },
      //点击当前的下标
      index: "", //当前点击的下标
      cancelIndex: 0,
      id: "" //点击当前的id
    };
  },
  //注册组件
  components: {
    Paging
  },
  created() {
    this.gain();
    //渲染spu树接口
    Recommend.spu().then(res => {
      this.spulist = res.data.data;
      this.getTreeData(this.spulist);
    });
  },
  filters: {
    //推荐商品过滤器
    recommendTypeMe(data) {
      if (data == 1) {
        return "橱窗推荐";
      } else {
        return "全部";
      }
    }
  },
  methods: {
    //点击取消按钮
    cancelbutton() {
      this.dialogFormVisible = false;
    },
    //获取列表数据内容
    gain() {
      var _this = this;
      Recommend.gain(_this.forms)
        .then(res => {
          _this.tableData = res.data.data.items; //将接口列表内容赋值给列表数组
          _this.forms.total = res.data.data.total; //分页————条数
          _this.forms.pageSize = res.data.data.pageSize; //页码长度
          _this.forms.pageNo = res.data.data.pageNo; //页码
        })
        .catch(err => {
          return err;
        });
    },
    //点击添加按钮触发
    add() {
      this.$nextTick(() => {
        if (this.$refs.form) {
          this.$refs.form.resetFields();
        }
      });
      this.bool = true; //bool为true触发添加接口
      this.form.type = ""; //将推荐类型清空
      this.form.goods = ""; //将推荐商品清空
      this.dialogFormVisible = true; //显示弹出框
    },
    //添加新角色接口
    adduser(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          let goods = this.$refs.dom.getCheckedNodes()[0].data.id; //获取到点击的商品id然后赋值
          var _this = this;
          Recommend.adduser(_this.form, goods)
            .then(res => {
              if (res.data.errmsg == "成功") {
                this.gain();
                this.Prosperity.Messages("添加成功", "success");
                this.dialogFormVisible = false;
              } else {
                this.Prosperity.Messages(res.data.errmsg, "error");
              }
            })
            .catch(err => {
              return err;
            });
        } else {
          return false;
        }
      });
    },
    //搜索信息
    seek() {
      this.gain();
    },
    //点击删除当前内容
    handleDelete(row) {
      this.form.type = row.recommendType;
      this.form.id = row.id; //当前删除的id
      this.$confirm("此操作将永久删除该商品，是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //确定删除的话
          this.cancel();
        })
        .catch(() => {
          if (document.getElementsByClassName("el-message").length === 0) {
            this.Prosperity.Messages("已取消删除");
          }
        });
    },
    //删除内容接口
    cancel() {
      var _this = this;
      Recommend.cancel(_this.form)
        .then(res => {
          if (res.data.errmsg == "成功") {
            _this.gain(); //重新渲染
            if (
              document.getElementsByClassName("el-notification").length === 0
            ) {
              this.Prosperity.Messages("删除角色成功", "success");
            }
            _this.dialogFormVisible = false;
          }
        })
        .catch(err => {
          return err;
        });
    },
    //递归
    getTreeData(data) {
      for (let item in data.length) {
        if (data[item].children.length < 1) {
          data[item].children = undefined;
        } else {
          this.getTreeData(data[item].children);
        }
      }
    }
  }
};
</script>

<style scoped="scoped">
/*最大父盒子开始*/
#system_role {
  padding: 20px;
}
/*最大父盒子结束*/

/*顶部按钮开始*/
.role_seek > .el-button--primary {
  line-height: 1px;
  font-size: 14px;
  border: 1px solid #409eff !important;
  background: #409eff !important;
  color: #fff;
  vertical-align: middle;
  padding: 12px 20px;
  margin-left: 5px;
}
/*顶部按钮结束*/

/*底部列表内容*/
.el-table {
  margin-top: 23px;
}
.cell {
  text-align: center;
  font-size: 12px;
  padding: 0px 10px;
}
.el-table__row > td {
  padding: 8px 0px;
}
.button_redact {
  background: #409eff !important;
  color: #fff !important;
  border: 1px solid #409eff;
} /*编辑内容按钮*/
.button_redact:hover {
  background: #66b1ff !important;
  border: 1px solid #66b1ff;
}
.button_remove {
  background: #f56c6c !important;
  color: #fff !important;
  border: #f56c6c 1px solid;
}
.button_remove:hover {
  background: #f78989 !important;
  border: 1px solid #f78989;
}
.el-table--scrollable-x .el-table__body-wrapper {
  overflow-x: hidden;
}
/*底部列表内容*/

/*底部分页*/
.block {
  margin: 55px 0px;
}
.pagination .el-input__suffix {
  left: 70px !important;
}
/*底部分页结束*/

/*添加弹出框*/
.el-form-item__label {
  text-align: left;
  margin-left: 50px;
  width: 85px !important;
  /* position: relative; */
}
.el-input__inner {
  text-indent: 5px;
}
.el-form {
  margin-left: 65px !important;
}
.el-form-item__label > .el-form-item__content > .el-form-item__error {
  padding: 50px !important;
  width: 100%;
}
</style>
